package com.example.stlink.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.stlink.R;
import com.example.stlink.model.constants.ModelFieldConstants;
import com.example.stlink.model.response.data.CourseDetailData;
import com.example.stlink.utils.CommonUtils;

public class CourseIntroMsgFragment extends Fragment {

    private static AppCompatActivity mAppCompatActivity;

    private static ImageView ivCourseImg;
    private static TextView tvCourseDescriptionMsg;
    private static TextView tvCourseCreateTimeMsg;
    private static TextView tvCourseStartTimeMsg;
    private static TextView tvCourseEndTimeMsg;
    private static TextView tvCourseStatusMsg;

    private static CourseDetailData courseDetailData;

    private static View mView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_course_intro_msg, container, false);
        initView(view);
        mView = view;
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    /**
     * 堆外暴露的一个方法，用于获取该碎片实例
     * @param str 说明
     * @param appCompatActivity 上下文
     * @return 未结课的碎片实例
     */
    public static CourseIntroMsgFragment newInstance(String str, AppCompatActivity appCompatActivity){
        CourseIntroMsgFragment courseIntroMsgFragment = new CourseIntroMsgFragment();
        mAppCompatActivity = appCompatActivity;

        return courseIntroMsgFragment;
    }


    /**
     * 绑定控件，初始化实例等
     * @param view 当前视图
     */
    private static void initView(View view) {

        ivCourseImg = view.findViewById(R.id.iv_course_img);
        tvCourseDescriptionMsg = view.findViewById(R.id.tv_course_description_msg);
        tvCourseCreateTimeMsg = view.findViewById(R.id.tv_course_create_time_msg);
        tvCourseStartTimeMsg = view.findViewById(R.id.tv_course_start_time_msg);
        tvCourseEndTimeMsg = view.findViewById(R.id.tv_course_end_time_msg);
        tvCourseStatusMsg = view.findViewById(R.id.tv_course_status_msg);
        //内容超出文本框大小，支持滑动
        tvCourseDescriptionMsg.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    /**
     * 刷新数据
     * @param intent 携带的数据
     */
    public static void freshData(Intent intent){

        courseDetailData = (CourseDetailData) intent.getExtras().getSerializable(ModelFieldConstants.COURSE_DETAIL_DATA);
        System.out.println("initView: courseDetailData = " + courseDetailData);

        if(courseDetailData == null){
            return;
        }
        String courseImg = courseDetailData.getCoursePhoto();
        String courseIntro = courseDetailData.getIntroduce();
        Long courseCreateTime = courseDetailData.getCreateTime();
        Long courseStartTime = courseDetailData.getStartTime();
        Long courseEndTime = courseDetailData.getEndTime();

        if(courseImg != null){
            Glide.with(mAppCompatActivity)
                    .load(courseImg)
                    .into(ivCourseImg);
        }
        if(courseIntro != null){
            tvCourseDescriptionMsg.setText(courseIntro);
        }
        if(courseCreateTime != null){
            String startFormDate = CommonUtils.stampToDate(String.valueOf(courseCreateTime), "yyyy年MM月dd日");
            tvCourseCreateTimeMsg.setText(startFormDate);
        }
        if(courseStartTime != null){
            String startFormDate = CommonUtils.stampToDate(String.valueOf(courseStartTime), "yyyy年MM月dd日");
            tvCourseStartTimeMsg.setText(startFormDate);
        }
        if(courseEndTime != null){
            String startFormDate = CommonUtils.stampToDate(String.valueOf(courseEndTime), "yyyy年MM月dd日");
            tvCourseEndTimeMsg.setText(startFormDate);
        }
        long currTime = System.currentTimeMillis();
        if(courseEndTime != null && currTime <= courseEndTime){
            tvCourseStatusMsg.setText("未完结");
        }else{
            tvCourseStatusMsg.setText("已完结");
        }
    }
}
